﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CMSMVC4.Areas.Admin.Models;
using CMSMVC4.Filters;

namespace CMSMVC4.Areas.Admin.Controllers
{
  
    [Authorize]
    public class SetGioiThieuController : Controller
    {
        //
        // GET: /Admin/SetGioiThieu/
        CMSMVC4Entities db = new CMSMVC4Entities();

        public ActionResult SetGioiThieu(SetGioiThieuModel model)
        {
            if (ModelState.IsValid)
            {
                var data = db.Pages.Where(x => x.Category.Type == MenuType.GioiThieu);
                foreach (var page in data)
                {
                    if (page.Id == model.idpage)
                    {
                        page.Status = PageStatus.GioiThieu;
                    }
                    else
                    {
                        page.Status = PageStatus.Thuong;
                    }
                }
                if (Request.UrlReferrer != null) return Redirect(Request.UrlReferrer.ToString());
            }
            return View("Error");
        }

        public ActionResult SetDichVu(SetDichVuModel model)
        {
            if (ModelState.IsValid)
            {
                var data = db.Pages.Where(x => x.Category.Type == MenuType.DichVu);
                foreach (var page in data)
                {
                    if (page.Id == model.idpage)
                    {
                        page.Status = PageStatus.GioiThieu;
                    }
                    else
                    {
                        page.Status = PageStatus.Thuong;
                    }
                }
                db.SaveChanges();
                if (Request.UrlReferrer != null) return Redirect(Request.UrlReferrer.ToString());
            }
            return View("Error");
        }

        public ActionResult SetTinTuc(SetDichVuModel model)
        {
            if (ModelState.IsValid)
            {
                var data = db.Pages.Where(x => x.Category.Type == MenuType.TinTuc);
                foreach (var page in data)
                {
                    if (page.Id == model.idpage)
                    {
                        page.Status = PageStatus.GioiThieu;
                    }
                    else
                    {
                        page.Status = PageStatus.Thuong;
                    }
                }
                db.SaveChanges();
                if (Request.UrlReferrer != null) return Redirect(Request.UrlReferrer.ToString());
            }
            return View("Error");
        }

        public ActionResult SetLienHe(SetLienHeVuModel model)
        {
            if (ModelState.IsValid)
            {
                var data = db.Pages.Where(x => x.Category.Type == MenuType.LienHe);
                foreach (var page in data)
                {
                    if (page.Id == model.idpage)
                    {
                        page.Status = PageStatus.GioiThieu;
                    }
                    else
                    {
                        page.Status = PageStatus.Thuong;
                    }
                }
                db.SaveChanges();
                if (Request.UrlReferrer != null) return Redirect(Request.UrlReferrer.ToString());
            }
            return View("Error");
        }

        public ActionResult SetThietBi(SetThietBiModel model)
        {
            if (ModelState.IsValid)
            {
                var data = db.Pages.Where(x => x.Category.Type == MenuType.ThiteBi);
                foreach (var page in data)
                {
                    if (page.Id == model.idpage)
                    {
                        page.Status = PageStatus.GioiThieu;
                    }
                    else
                    {
                        page.Status = PageStatus.Thuong;
                    }
                }
                db.SaveChanges();
                if (Request.UrlReferrer != null) return Redirect(Request.UrlReferrer.ToString());
            }
            return View("Error");
        }
    }
}
